﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace vfife_building_algorithm
{
    public partial class Form_Modeling : Form
    {      
        public Hashtable htNodes = new Hashtable();
        public Hashtable htElmts = new Hashtable();

        private Main_Frame paf;

        public Form_Modeling(Main_Frame parent)
        {
            InitializeComponent();

            paf = parent;

            loadNodes();
            
            bPrev.Enabled = false;
            bNext.Enabled = true;
            bFinish.Enabled = false;
        }

        private void bNext_Click(object sender, EventArgs e)
        {
            switch (tabControlModeling.SelectedIndex) { 
                
                //Nodes defined, go to define elements              
                case 0:
                    if(submitNodes())
                    {
                        loadElmts();
                        tabControlModeling.SelectedIndex = 1;

                        bPrev.Enabled = true;
                        bNext.Enabled = false;
                        bFinish.Enabled = true;
                    }
                    break;

                case 1:
                    
                    break;
               
                default:
                    break;
            }     
        }

        private void bPrev_Click(object sender, EventArgs e)
        {
            switch (tabControlModeling.SelectedIndex)
            {
                case 1:
                    tabControlModeling.SelectedIndex = 0;

                    bPrev.Enabled = false;
                    bNext.Enabled = true;
                    bFinish.Enabled = false;
                    break;

                case 2:
                   // tabControlModeling.SelectedIndex = 1;

                   // bNext.Enabled = true;
                   // bFinish.Enabled = false;
                    break;

                default:
                    break;
            }    
        }

        private void bFinish_Click(object sender, EventArgs e)
        {
            if (submitElmts())
            {
                Model.Elements = htElmts;
                Model.Isset = true;
                Model.SetConNodes();
                paf.drawModel();

                this.Close();
            }
        }

        
        /// <summary>
        /// this is needed when the model is loaded in
        /// </summary>
        private void loadNodes()
        { 
        
        }
        
        /// <summary>
        /// list the nodes id in the elements tag
        /// </summary>
        private void loadElmts() 
        {
            // set value to combox
            DataGridViewComboBoxColumn cmboxNode1 = dataGridViewElmts.Columns[1] as DataGridViewComboBoxColumn;
            cmboxNode1.Items.Clear();
            DataGridViewComboBoxColumn cmboxNode2 = dataGridViewElmts.Columns[2] as DataGridViewComboBoxColumn;
            cmboxNode2.Items.Clear();
            foreach (DictionaryEntry de in htNodes)
            {
                cmboxNode1.Items.Add(de.Key.ToString());
                cmboxNode2.Items.Add(de.Key.ToString());
            }
        }

        /// <summary>
        /// leave the node definition tag
        /// TODO: Check the data validity & ID confict
        /// </summary>
        private Boolean submitNodes()
        {
            //clear the node list
            htNodes.Clear();

            int iRowNb = dataGridViewNodes.RowCount;
            for (int i = 0; i < iRowNb; i++)
            {
                DataGridViewRow row = dataGridViewNodes.Rows[i];
                
                //check if there exists empty value(s)
                if (null == row.Cells[0].Value || null == row.Cells[1].Value ||
                    null == row.Cells[2].Value || null == row.Cells[3].Value)
                { continue; }

                int nodeId = int.Parse(row.Cells[0].Value.ToString());
                double xcoord = double.Parse(row.Cells[1].Value.ToString());
                double ycoord = double.Parse(row.Cells[2].Value.ToString());
                double mass = double.Parse(row.Cells[3].Value.ToString());
                
                Node node = new Node(nodeId, xcoord, ycoord, mass);
                htNodes.Add(node.Id, node);
            }

            if (htNodes.Count < 2)
            {
                MessageBox.Show("Input at least two nodes");
                return false;
            }

            return true;
        }

        /// <summary>
        /// set elements to data structure
        /// </summary>
        /// <returns></returns>
        private Boolean submitElmts()
        {
            htElmts.Clear();

            int iRowNb = dataGridViewElmts.RowCount;
            for (int i = 0; i < iRowNb; i++)
            {
                DataGridViewRow row = dataGridViewElmts.Rows[i];

                //check if there exists empty value(s)
                if (null == row.Cells[0].Value || null == row.Cells[1].Value ||
                    null == row.Cells[2].Value || null == row.Cells[3].Value ||
                    null == row.Cells[4].Value || null == row.Cells[5].Value)
                { continue; }

                int elmntId = int.Parse(row.Cells[0].Value.ToString());
                
                int nodeId1 = int.Parse(row.Cells[1].Value.ToString());
                int nodeId2 = int.Parse(row.Cells[2].Value.ToString()); 
              
                Node nodeSelected1 = (Node)htNodes[nodeId1];
                Node nodeSelected2 = (Node)htNodes[nodeId2];

                double densityInput = double.Parse(row.Cells[3].Value.ToString());
                double areaInput = double.Parse(row.Cells[4].Value.ToString());
                double EAInput = double.Parse(row.Cells[5].Value.ToString());   // young's module

                Element element = new Element(elmntId, nodeSelected1, nodeSelected2, densityInput, areaInput);
                Material material = new Material(EAInput);
                element.Material = material;
                htElmts.Add(element.Id, element);
            }

            if (htElmts.Count < 1)
            {
                MessageBox.Show("Input at least one element");
                return false;
            }

            return true;
        }
    }
}
